package com.atguigu.service_edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.service_edu.client.VodClient;
import com.atguigu.service_edu.entity.EduChapter;
import com.atguigu.service_edu.entity.EduCourse;
import com.atguigu.service_edu.entity.EduTeacher;
import com.atguigu.service_edu.entity.vo.CourseInfoVo;
import com.atguigu.service_edu.entity.vo.EduCourseQuery;
import com.atguigu.service_edu.entity.vo.EduPublishVo;
import com.atguigu.service_edu.service.EduChapterService;
import com.atguigu.service_edu.service.EduCourseDescriptionService;
import com.atguigu.service_edu.service.EduCourseService;
import com.atguigu.service_edu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author clark
 * @since 2020-04-10
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @ApiOperation(value = "添加课程基本信息的方法")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(
            @ApiParam(name = "CourseInfoVo",value="课程信息对象",required = false)
            @RequestBody CourseInfoVo courseInfoVo
            ){
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    @ApiOperation(value = "更新课程")
    @PutMapping("/updateCourseInfo/{id}")
    public R updateCourseInfo(
            @ApiParam(name = "id",value="课程ID",required = true)
            @PathVariable
            String id,
            @ApiParam(name = "CourseInfoVo",value="课程信息对象",required = false)
            @RequestBody CourseInfoVo courseInfoVo
    ){
        courseInfoVo.setId(id);

        id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }



    @ApiOperation(value="根据ID获取课程基本信息")
    @GetMapping("/getCourseInfo/{id}")
    public R getCourseInfoById(
            @ApiParam(name="id",value = "课程Id",required = true)
            @PathVariable String id){
        CourseInfoVo courseInfo = eduCourseService.getCourseInfo(id);
        return R.ok().data("courseInfo",courseInfo);
    }

    @ApiOperation(value="根据ID获取课程发布信息")
    @GetMapping("/getPublishInfo/{id}")
    public R getPublishInfo(@PathVariable String id){
        EduPublishVo eduPublishVo = eduCourseService.getPublishInfo(id);
        return R.ok().data("eduPublishVo",eduPublishVo);
    }


    @PutMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    @PostMapping("/getCourseListByPage/{page}/{limit}")
    public R getCourseListByPage( @ApiParam(name = "page",value = "当前页码" , required = true)
                       @PathVariable Long page,
                       @ApiParam(name = "limit",value = "每页记录数" ,required = true)
                       @PathVariable Long limit,
                       @RequestBody EduCourseQuery educCourseQuery
    ){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        eduCourseService.pageQuery(pageParam,educCourseQuery);
        List<EduCourse> eduCourseList = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total",total).data("rows",eduCourseList);
    }

    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }







}

